﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using SheepCommon.API.Seedweanings;

namespace SheepCommon.API.BreedingHistoryss
{
    public class BreedingHistorysCreateHander : IRequestHandler<BreedingHistorysCreateCommand, Result>
    {
        private readonly IBaseRepository<BreedingHistorys> _breedingHistorysRepository;
        private readonly IMapper _mapper;

        public BreedingHistorysCreateHander(IBaseRepository<BreedingHistorys> breedingHistorysRepository, IMapper mapper)
        {
            _breedingHistorysRepository = breedingHistorysRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 种母历史记录新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BreedingHistorysCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var breedingHistorys = _mapper.Map<BreedingHistorys>(request);
                breedingHistorys.CreatedOwnerId = "张三";
                breedingHistorys.CreatedTime = DateTime.Now;
                var list = await _breedingHistorysRepository.Add(breedingHistorys);
                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
